/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2009 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/

#ifndef CELL_OSK_CAPTION
#include "../SDL_cellvideo.h"
void							CELL_CAPTION_Draw					()
{
}

void							CELL_CAPTION_Set					(_THIS, const char* aCaption, const char* aIcon)
{
}
#else

#include "SDL_config.h"
#include "../SDL_cellvideo.h"
#include "../SDL_celloverlayimage.h"
#include <SDL/SDL_ttf.h>

/* Like the libraries automatically with SNC */
#ifdef __SNC__
#pragma comment (lib,"SDL_ttf")
#pragma comment (lib,"freetype")
#endif

/* Include the font data */
#define BUILDING_CELLPADMAP
#include "../SDL_cellfont.h"
#undef BUILDING_CELLPADMAP

/* Helpers */
static void						LoadFont							()
{
	if(!CELL_Video.Caption.Font)
	{
		TTF_Init();
		CELL_Video.Caption.Font = TTF_OpenFontRW(SDL_RWFromMem((void*)CELL_ttfliberation, sizeof(CELL_ttfliberation)), 1, 20);
	}
}

static void						UpdateImage							()
{
	if(CELL_Video.Caption.Update)
	{
		static SDL_Color color={0,0,0}, bgcolor={0xff,0xff,0xff,0xff};
		SDL_Surface* textsurface = TTF_RenderText_Shaded(CELL_Video.Caption.Font, strlen(CELL_Video.Caption.Caption) ? CELL_Video.Caption.Caption : "NO CAPTION", color, bgcolor);
		SDL_Surface* textrender = SDL_CreateRGBSurface(0, 640, textsurface->h, 32, 0x00FF0000, 0xFF00, 0xFF, 0xFF000000);
		SDL_FillRect(textrender, 0, 0xFFFFFFFF);
		SDL_BlitSurface(textsurface, 0, textrender, 0);

		CELL_IMAGE_Load(&CELL_Video.Caption.Image, 0, 0, 1200, 20, (void*)textrender->pixels, 640, textsurface->h);

		SDL_FreeSurface(textsurface);
		SDL_FreeSurface(textrender);

		CELL_Video.Caption.Update = 0;
	}
}

/* Implementation */
void							CELL_CAPTION_Draw					()
{
	LoadFont();
	UpdateImage();

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrthof(0, 640, 480, 0, -1, 1);

	CELL_IMAGE_Draw(&CELL_Video.Caption.Image);

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}

void							CELL_CAPTION_Set					(_THIS, const char* aCaption, const char* aIcon)
{
	if(aCaption && strncmp(aCaption, CELL_Video.Caption.Caption, 256))
	{
		strncpy(CELL_Video.Caption.Caption, aCaption, 256);
		CELL_Video.Caption.Update = 1;
	}

	if(aIcon)
	{
		strncpy(CELL_Video.Caption.Icon, aIcon, 256);
	}
}

#endif

